// 碳排走势卡

var timeFormatMap = {
    'year': "%Y-%m",
    'month': "%Y-%m-%d",
    'day': "%Y-%m-%d %H",
    'hour': "%Y-%m-%d %H:%m"
};
var timeSingleFormatMap = {
    'year': "%m",
    'month': "%d",
    'day': "%H",
    'hour': "%m"
};

dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    var db = iot.db();
    var format,singleFormate;
    // var electricityResult = null;
    // var gasResult = null;
    // 2019之后的数据
    var carbonEq2019Result = null;

    // var timeSub = parseInt(theTime.substring(0,4));

    var resultData = {
        xData : iot.list(),
        yLeft: iot.list(),
        yRight : iot.list()
    };

    var timeType = iot.getTimeByFormat(theTime);
    var timeDataList = getTimeList(theTime);
    format = timeFormatMap[timeType];
    singleFormate = timeSingleFormatMap[timeType];

    // if(timeSub >= 2019 || factoryName === 'PV'){
    var fuSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
        " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
        " SUM(dev_win.dvalue) totalSum" +
        " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 2 and field_type = '用量' and energy_struc_id = " + workSpace + ") esp" +
        " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '" + theTime + "%' group by time";
    carbonEq2019Result = db.qry(fuSql);

    // }else{
    //     // E001._用量 * 碳排的电系数 +  G001.用量 * 碳排的燃气系数
    //     if(frameworkIdToDevSn[factoryName] && frameworkIdToDevSn[factoryName][workSpace] && frameworkIdToDevSn[factoryName][workSpace].ele){
    //         var doSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
    //             " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
    //             " SUM(dev_win.dvalue) totalSum" +
    //             " FROM iot_dev_data_win dev_win " +
    //             " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
    //             " where 1=1 " +
    //             " and dev.sn='" + frameworkIdToDevSn[factoryName][workSpace].ele + "'" +
    //             " and dev_win.data_type = '_用量'" +
    //             " and dev_win.the_time like '" + theTime + "%' group by time";
    //         electricityResult = db.qry(doSql);
    //     }
    //
    //     if(frameworkIdToDevSn[factoryName] && frameworkIdToDevSn[factoryName][workSpace] && frameworkIdToDevSn[factoryName][workSpace].gas){
    //         gasResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
    //             " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
    //             " SUM(dev_win.dvalue) totalSum" +
    //             " FROM iot_dev_data_win dev_win " +
    //             " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
    //             " where 1=1 " +
    //             " and dev.sn='"+frameworkIdToDevSn[factoryName][workSpace].gas+"'" +
    //             " -- and dev_win.data_type = '用量'\n" +
    //             " and dev_win.the_time like '"+ theTime +"%' group by time");
    //     }
    //
    // }

    var yieldId = getFactoryYieldId(factoryName)
    // 产量
    var yieldResult = db.qry("select DATE_FORMAT(the_time,'" + format + "') time," +
        " DATE_FORMAT(the_time,'" + singleFormate + "') timeIx," +
        " SUM(dvalue) totalSum from iot_dev_data_win" +
        " where " +
        " if("+ yieldId +" = 10687,data_type = '_PV产量' ,1=1)\n" +
        " and dev_id = "+ yieldId +" and the_time like '"+ theTime +"%' group by time");

    // 通过timeType获取当前时间
    var result = getCurrentTimeByType(timeType);
    var typeTime = result["typeTime"];
    var currTime = result["currTime"];

    // if(timeSub >= 2019){

        for(var ix in timeDataList){
            var timeItem = timeDataList[ix];
            var data  = 0;
            carbonEq2019Result.forAll(function (item,ix) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    data = parseInt(item.totalSum);
                }
            })

            var totalYield = 0;
            yieldResult.forAll(function (item) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    totalYield = item.totalSum;
                }
            });

            //判断当前选择总数还是单车
            if (totalOrSingle.equals("total")) {
                resultData.xData.add(timeItem);
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                resultData.yLeft.add(data);
            }else if (totalOrSingle.equals("single")) {
                resultData.xData.add(timeItem);
                if (data && totalYield){
                    data = data/totalYield;
                    data = parseInt(data);
                }
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                if (totalYield === 0){
                    resultData.yLeft.add(0);
                }else{
                    resultData.yLeft.add(data);
                }
            }
        }

    // }else{
    //
    //     for(var ix in timeDataList){
    //         var timeItem = timeDataList[ix];
    //         var data  = 0;
    //
    //         if (electricityResult != null) {
    //             electricityResult.forAll(function (item, ix) {
    //                 if (item && item.timeIx && item.timeIx === timeItem) {
    //                     data = item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].ele : 0);
    //                 }
    //             })
    //         }
    //         if (gasResult != null) {
    //             gasResult.forAll(function (item, ix) {
    //                 if (item && item.timeIx && item.timeIx === timeItem) {
    //                     data = parseInt(data + (item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].gas : 0)));
    //                 }
    //             })
    //         }
    //
    //         var totalYield = 0;
    //         yieldResult.forAll(function (item) {
    //             if(item && item.timeIx && item.timeIx === timeItem){
    //                 totalYield = item.totalSum;
    //             }
    //         });
    //         //判断当前选择总数还是单车
    //         if (totalOrSingle.equals("total")) {
    //             resultData.xData.add(timeItem);
    //             // 把当前时间之后的数据干掉
    //             if (theTime == currTime && timeItem > typeTime) {
    //                 continue;
    //             }
    //             resultData.yLeft.add(data);
    //         }else if (totalOrSingle.equals("single")) {
    //             resultData.xData.add(timeItem);
    //             // 把当前时间之后的数据干掉
    //             if (theTime == currTime && timeItem > typeTime) {
    //                 continue;
    //             }
    //             if (data && totalYield){
    //                 data = data/totalYield;
    //                 data = parseInt(data);
    //             }
    //             if (totalYield === 0){
    //                 resultData.yLeft.add(0);
    //             }else{
    //                 resultData.yLeft.add(data);
    //             }
    //         }
    //     }
    //
    // }

    for(var ix in timeDataList){
        var timeItem = timeDataList[ix];
        var data  = 0;
        /*yieldResult.forAll(function (item,ix) {
            if(item && item.timeIx && item.timeIx === timeItem){
                data = item.totalSum;
            }
        })*/
        // 把当前时间之后的数据干掉
        /*if (theTime == currTime && timeItem > typeTime) {
            continue;
        }*/
        resultData.yRight.add(data);
    }
    resp.setJsData(resultData);
}